Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the redirectUrl argument and use it in loginUrl #7

Merged
merged 5 commits into from
Feb 12, 2024

Conversation

gumaerc
Copy link
Collaborator

@gumaerc gumaerc commented Feb 5, 2024

What are the relevant tickets?

Closes #6

Description (What does it do?)

This PR adds a new argument to initLoginButton, redirectUrl. This string argument is simply passed through as the redirect_url query string parameter to the login endpoint. If the domain of the redirect URL is set in the SOCIAL_AUTH_ALLOWED_REDIRECT_HOSTS setting on the instance of mit-open that you are hitting, then you will be redirected back to that URL after logging in.

How can this be tested?

  • You will need to make a slight alteration to the code before testing this, as at the time of writing add trailing slash to currentUserUrl #5 has not been merged and it fixes an issue with the /users/me endpoint:
Replace:
const apiUrl = `${parsedBaseUrl.origin}/api/v0/users/me?format=json`
With:
const apiUrl = `${parsedBaseUrl.origin}/api/v0/users/me/?format=json`
  • In order to test this, you will need a running instance of mit-open. Follow the instructions in the mit-open readme for initial setup. You will need an account in your local instance for this to work. Your instance of mit-open should be accessible at http://od.odl.local:8063. You will need to set the following environment variables:
CORS_ALLOWED_ORIGINS=["http://ocw.odl.local:3000"]
SOCIAL_AUTH_ALLOWED_REDIRECT_HOSTS=["ocw.odl.local:3000"]
  • You will also need ocw-hugo-themes (https://github.com/mitodl/ocw-hugo-themes) set up locally, running a course using yarn start course. Follow the readme there for basic setup instructions and make sure you can start up a course. After you have that working, check out the cg/add-mit-open-login-button branch. Assuming you have cloned this repo, run yarn add /path/to/mit-open-login-button to add your local copy of the code for this to your locally running ocw-hugo-themes. This is necessary because the package is not yet published to NPM. You will also need an entry in your hosts file that points the ocw.odl.local domain to 127.0.0.1.
  • Once you have all this running, make sure you are logged out of your local instance of mit-open
  • Back in your locally running OCW course site, you should see a Login button in the upper right. Click this button and log in to your locally running mit-open
  • After you have logged in, you will be sent back to http://ocw.odl.local:3000
  • Confirm that you see Logged in as: username with your username in place of username
  • In another tab, log out of your locally running mit-open
  • Back on the local OCW page, refresh and verify that the login button reappears

@rhysyngsun rhysyngsun self-assigned this Feb 6, 2024
Copy link

@rhysyngsun rhysyngsun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review

index.js Outdated
buttonText = "Login",
buttonClass = "",
loggedInTextClass = "",
) {
const container = document.getElementById(containerId)
const parsedBaseUrl = new URL(baseUrl)
const apiUrl = `${parsedBaseUrl.origin}/api/v0/users/me?format=json`
const loginUrl = `${parsedBaseUrl.origin}/login/ol-oidc/`
const redirectUrlParam =
redirectUrl !== "" ? `?redirect_url=${redirectUrl}` : ""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be encoded:

Suggested change
redirectUrl !== "" ? `?redirect_url=${redirectUrl}` : ""
redirectUrl !== "" ? `?redirect_url=${encodeURIComponent(redirectUrl)}` : ""

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I implemented your suggestion

@gumaerc gumaerc added the Needs Review An open Pull Request that is ready for review label Feb 6, 2024
@gumaerc
Copy link
Collaborator Author

gumaerc commented Feb 8, 2024

@rhysyngsun This is ready for another look

@rhysyngsun
Copy link

Note: I'm not sure if there will be docs elsewhere but the instructions were missing the step to set the environment variable MIT_OPEN_BASEURL for the ocw-hugo-themes process.

@gumaerc
Copy link
Collaborator Author

gumaerc commented Feb 12, 2024

Note: I'm not sure if there will be docs elsewhere but the instructions were missing the step to set the environment variable MIT_OPEN_BASEURL for the ocw-hugo-themes process.

Yea, sorry about that. @ChristopherChudzicki merged a PR into my branch for this that adds that variable and I forgot to update the testing instructions.

Copy link

@rhysyngsun rhysyngsun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rhysyngsun rhysyngsun added Waiting on Author and removed Needs Review An open Pull Request that is ready for review labels Feb 12, 2024
@gumaerc gumaerc merged commit ee2f584 into main Feb 12, 2024
2 checks passed
@gumaerc gumaerc deleted the cg/add-redirect-url branch February 12, 2024 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add redirect_url query string parameter
2 participants